package com.pig.openg1.data;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.Matrix;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.Random;

public class Cube {
    private final String TAG = Cube.class.getSimpleName();

//    private Context mContext;
    private Random mRandom = new Random();
//
    /**
     * 坐标点
     */
    private float[] cubeVer = {
            //         数组1 和 索引2
            -1.0f, 1.0f, 1.0f,//索引 0
            -1.0f, -1.0f, 1.0f,//索引 1
            1.0f, -1.0f, 1.0f,//索引 2
            1.0f, 1.0f, 1.0f,//索引 3

            -1.0f, 1.0f, -1.0f,//索引 4
            -1.0f, -1.0f, -1.0f,//索引 5
            1.0f, -1.0f, -1.0f,//索引 6
            1.0f, 1.0f, -1.0f,//索引 7

    };


    /***
     * 顶点
     */
    private short index[]={
            0, 3, 2, 0, 2, 1,//正面
            6, 5, 1, 6, 1, 2,//下面
            0, 7, 3, 0, 4, 7,//上面
            6, 7, 4, 6, 4, 5,//后面
            0, 1, 5, 0, 5, 4,//左面
            6, 3, 7, 6, 2, 3//右面
    };

    private float[] getColor(){

        mRandom = new Random();
        float color[] = new float[index.length * 4 ];
        int m = 0 ;
        for(int i=0;i<index.length;i++)
        {
            int R = mRandom.nextInt(3);
            int G = mRandom.nextInt(3);
            int B = mRandom.nextInt(3);
            color[m++] = R;
            color[m++] = G;
            color[m++] = B;
            color[m++] = 1;
        }
        return color;
    };
//
    public static final String verTexShader = ""+
//            "attribute vec4 vPosition;"+
//            "uniform mat4 vMatrix;" +
//            "attribute vec4 vColor;"+
//            "varying vec4 aColor;"+
//            "void main(){ gl_Position = vMatrix* vPosition; aColor = vColor; }";
            "attribute vec4 vPosition;" +
            "uniform mat4 vMatrix;" +
            "attribute vec4 vColor;" +
            "varying vec4 aColor;" +
            "void main(){" +
            "gl_Position =vMatrix*vPosition ;" +
            "aColor=vColor;" +
            "}";
    /**
     * 片元着色器
     */
    public static final String fragShader = ""+
//            "precision mediump float;" +  //默认浮点精度修饰符
//            "varying vec4 aColor;"+
//            "void main(){ gl_FragColor = aColor; }";
            "precision mediump float;" +
            "varying vec4 aColor;" +
            "void main(){" +
            "gl_FragColor=aColor;" +
            "}";


    private FloatBuffer verTexBuffer,colorBuffer;
    private ShortBuffer indexBuffer;
    private int mProgram;
    private int vPosition,vMatrix,vColor;
    public float angle = 0F;

    public static float[] mProjMatrix = new float[16];
    public static float[] mVMatrix = new float[16];
    public static float[] mMVPMatrix;
    public static float[] mMMatrix = new float[16];

    public Cube()
    {
        verTexBuffer = getFloatBuffer(cubeVer);
        colorBuffer = getFloatBuffer(getColor());
        indexBuffer = getShortBuffer(index);

        mProgram = GLES20.glCreateProgram();
        int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER,verTexShader);
        int fragShaders = loadShader(GLES20.GL_FRAGMENT_SHADER,fragShader);
        GLES20.glAttachShader(mProgram,vertexShader);
        GLES20.glAttachShader(mProgram,fragShaders);
        //连接程序
        GLES20.glLinkProgram(mProgram);
        vPosition = GLES20.glGetAttribLocation(mProgram,"vPosition");
        vColor = GLES20.glGetAttribLocation(mProgram,"vColor");
        vMatrix = GLES20.glGetUniformLocation(mProgram,"vMatrix");

    }

    private int loadShader(int type,String source)
    {
        int shader = 0;
        shader = GLES20.glCreateShader(type);
        if(shader != 0 )
        {
            GLES20.glShaderSource(shader,source);
            GLES20.glCompileShader(shader);
            int[] compiled = new int[1];
            GLES20.glGetShaderiv(shader,GLES20.GL_COMPILE_STATUS,compiled,0);
            if(compiled[0] == 0 )
            {
                Log.e(TAG,GLES20.glGetShaderInfoLog(shader));
                GLES20.glDeleteShader(shader);
                shader = 0;
            }
        }
        return shader;
    }

    private FloatBuffer getFloatBuffer(float[] buffer)
    {
        ByteBuffer mByteBuffer = ByteBuffer.allocateDirect(buffer.length * 4);
        mByteBuffer.order(ByteOrder.nativeOrder());
        FloatBuffer mFloatBuffer = mByteBuffer.asFloatBuffer();
        mFloatBuffer.put(buffer);
        mFloatBuffer.position(0);
        return mFloatBuffer;
    }

    private ShortBuffer getShortBuffer(short[] buffer)
    {
        ByteBuffer mByteBuffer = ByteBuffer.allocateDirect(buffer.length * 2);
        mByteBuffer.order(ByteOrder.nativeOrder());
        ShortBuffer mFloatBuffer = mByteBuffer.asShortBuffer();
        mFloatBuffer.put(buffer);
        mFloatBuffer.position(0);
        return mFloatBuffer;
    }

    public void drawSelf()
    {
        GLES20.glUseProgram(mProgram);
//        Matrix.setRotateM(mMMatrix,0,0,0,1,0);
//        Matrix.translateM(mMMatrix,0,0,0,1);
        Matrix.setRotateM(mMMatrix,0,angle,1F,1F,0F);
        GLES20.glUniformMatrix4fv(vMatrix,1,false,getMatrix(mMMatrix),0);
        //设置数据
        GLES20.glVertexAttribPointer(vPosition,3,GLES20.GL_FLOAT,false,0,verTexBuffer);
        GLES20.glVertexAttribPointer(vColor,4,GLES20.GL_FLOAT,false
        ,0,colorBuffer);

        //开启顶点和颜色的绘制
        GLES20.glEnableVertexAttribArray(vPosition);
        GLES20.glEnableVertexAttribArray(vColor);
        //绘制
        GLES20.glDrawElements(GLES20.GL_TRIANGLES,36,GLES20.GL_UNSIGNED_SHORT,indexBuffer);

        //回收内存
        GLES20.glDisableVertexAttribArray(vPosition);
        GLES20.glDisableVertexAttribArray(vColor);


    }

    private float[] getMatrix(float[] params) {
        mMVPMatrix = new float[16];
        Matrix.multiplyMM(mMVPMatrix,0,mVMatrix,0,params,0);
        Matrix.multiplyMM(mMVPMatrix,0,mProjMatrix,0,mMVPMatrix,0);
        return mMVPMatrix;
    }

}
